﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Microsoft.Practices.Unity;
using LCA.Community.DAL.Interfaces;

namespace LCA.Community.Web.Controllers
{
    /// <summary>
    /// Base controller that contains basic operations shared by controllers.
    /// </summary>
    public abstract class BaseController : Controller
    {

        /// <summary>
        /// Gets the user ID.
        /// </summary>
        protected string UserID
        {
            get
            {
                try
                {
                    return (string)Session["ID"];
                }
                catch
                {
                    return null;
                }
            }
        }

        /// <summary>
        /// Determines whether the specified user is owner.
        /// </summary>
        /// <param name="userID">The user ID.</param>
        /// <returns>
        ///   <c>true</c> if the specified user is owner; otherwise, <c>false</c>.
        /// </returns>
        protected bool IsOwner(string userID)
        {
            return UserID == null ? false : UserID.Equals(userID, StringComparison.Ordinal);
        }

        /// <summary>
        /// Determines whether the specified user is friend.
        /// </summary>
        /// <param name="userID">The user ID.</param>
        /// <returns>
        ///   <c>true</c> if the specified user is friend; otherwise, <c>false</c>.
        /// </returns>
        protected bool IsFriend(string userID)
        {
            try
            {
                return FriendshipsRepository.AreFriends(UserID, userID);
            }
            catch (Exception)
            {
                return false;
            }
        }

        /// <summary>
        /// Determines whether the user is group owner of specified group.
        /// </summary>
        /// <param name="groupID">The group ID.</param>
        /// <returns>
        ///   <c>true</c> if the user is group owner of specified group; otherwise, <c>false</c>.
        /// </returns>
        protected bool IsGroupOwner(string groupID)
        {
            try
            {
                return GroupsRepository.Get(groupID).Owner.Equals(UserID, StringComparison.Ordinal);
            }
            catch (Exception)
            {
                return false;
            }
        }

        /// <summary>
        /// Determines whether the user is group member of specified group.
        /// </summary>
        /// <param name="userID">The user ID.</param>
        /// <returns>
        ///   <c>true</c> if the user is group member of specified group; otherwise, <c>false</c>.
        /// </returns>
        protected bool IsGroupMember(string groupID)
        {
                return GroupMembershipRepository.Get(groupID, UserID) != null;
        }

        /// <summary>
        /// Determines whether the specified user is admin.
        /// </summary>
        /// <param name="userID">The user ID.</param>
        /// <returns>
        ///   <c>true</c> if the specified user is admin; otherwise, <c>false</c>.
        /// </returns>
        protected bool IsAdmin(string userID)
        {
            try
            {
                return AdminsRepository.IsAdmin(userID);
            }
            catch (Exception)
            {
                return false;
            }            
        }

        // Repositories for accessing data
        [Dependency]
        public IGroupMembershipsRepository GroupMembershipRepository { get; set; }
        [Dependency]
        public IFriendshipsRepository FriendshipsRepository { get; set; }
        [Dependency]
        public IGroupsRepository GroupsRepository { get; set; }
        [Dependency]
        public IAdminsRepository AdminsRepository { get; set; }
    }
}
